<template>
  <div class="app-container">
    <SearchFilter size="medium" :maxShow="3" @search="handleQuery" @reset="resetQuery">
      <el-form :model="queryParams" label-position="right" label-width="70px" ref="queryForm">
        <el-row  :gutter="20">
          <el-col :span="7">
            <el-form-item label="工单编号" prop="equipmentId">
              <el-input v-model="queryParams.equipmentId" placeholder="请输入设备id" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="项目编号" prop="projectId">
              <el-input v-model="queryParams.projectId" placeholder="请输入项目id" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="设备名称" prop="areaId">
              <el-input v-model="queryParams.areaId" placeholder="请输入片区id" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="所属项目" prop="position">
              <el-input v-model="queryParams.position" placeholder="请输入定位" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="所属片区" prop="endDate">
              <el-date-picker clearable v-model="queryParams.endDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择截止时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="维修人员" prop="owner">
              <el-input v-model="queryParams.owner" placeholder="请输入业主负责人" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="设备类型" prop="ownerUnit">
              <el-input v-model="queryParams.ownerUnit" placeholder="请输入业主单位" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="设备厂商" prop="unitArea">
              <el-input v-model="queryParams.unitArea" placeholder="请输入单位地址" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="项目经理" prop="ownerPhone">
              <el-input v-model="queryParams.ownerPhone" placeholder="请输入业主电话" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="工单状态" prop="contractDate">
              <el-date-picker clearable v-model="queryParams.contractDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择合同时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="项目经理" prop="ownerPhone">
              <el-input v-model="queryParams.ownerPhone" placeholder="请输入业主电话" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </SearchFilter>

    <div class="content-container">
      <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
        <el-col :span="1.5">
          <p class="header-title">巡检工单列表</p>
        </el-col>
        <div>
          <el-col :span="1.5" v-if="ids.length">
            <el-button
              size="small"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['work:patrol:remove']"
            >批量删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="small"
              @click="handleAdd"
              v-hasPermi="['work:patrol:add']"
            >添加工单</el-button>
          </el-col>
        </div>
      </el-row>
  
      <el-table v-loading="loading" :data="patrolList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="主键" align="center" width="120" prop="id" /> -->
        <el-table-column label="设备" align="center" width="120" prop="equipmentName" />
        <el-table-column label="项目" align="center" width="120" prop="projectName" />
        <el-table-column label="片区" align="center" width="120" prop="areaName" />
        <el-table-column label="定位" align="center" width="120" prop="position" />
        <el-table-column label="截止时间" align="center" prop="endDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="业主负责人" align="center" width="120" prop="owner" />
        <el-table-column label="业主单位" align="center" width="120" prop="ownerUnit" />
        <el-table-column label="单位地址" align="center" width="120" prop="unitArea" />
        <el-table-column label="业主电话" align="center" width="120" prop="ownerPhone" />
        <el-table-column label="合同时间" align="center" prop="contractDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.contractDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="合同到期时间" align="center" prop="contractEndDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.contractEndDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目经理" align="center" width="120" prop="projectManager" />
        <el-table-column label="项目经理电话" align="center" width="120" prop="projectManagerPhone" />
        <!-- <el-table-column label="维修人员" align="center" width="120" prop="repairPerson" />
        <el-table-column label="维修人员电话" align="center" width="120" prop="repairPersonPhone" /> -->
        <el-table-column label="备注" align="center" width="120" prop="remark" />
        <el-table-column label="报修图片" align="center" width="120" prop="repairImg" />
        <!-- <el-table-column label="报修电话" align="center" width="120" prop="repairPhone" />
        <el-table-column label="报修备注" align="center" width="120" prop="repairRemark" /> -->
        <el-table-column label="工单状态" align="center" width="120" prop="status">
          <template slot-scope="scope">
            <div style="display: flex; align-items: center; justify-content: center;">
              <span class="wait-round" v-if="scope.row.status === 0"></span>
              <span class="already-round" v-if="scope.row.status === 1"></span>
              <span>{{changeStatus(scope.row.status)}}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="工单标签" align="center" width="120" prop="reStatus">
          <template slot-scope="scope">
            <div style="display: flex; align-items: center; justify-content: center;">
              <span class="wait-round" v-if="['申请延期', '拒绝延期', '同意延期'].includes(changeReStatus(scope.row.statusText))"></span>
              <!-- <span class="time-out-round " v-if="scope.row.status1 === 1"></span> -->
              <span class="delay-round " v-if="['申请退单', '拒绝退单', '同意退单'].includes(changeReStatus(scope.row.statusText))"></span>
              <span>{{changeReStatus(scope.row.statusText)}}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="220" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              class="text-text table-btn"
              v-hasPermi="['work:patrol:query']"
              @click="getRouterInfo(scope.row)"
            >查看</el-button>
            <el-button
              size="mini"
              type="text"
              class="text-danger table-btn"
              @click="handleDelete(scope.row)"
              v-hasPermi="['work:patrol:remove']"
            >删除</el-button>
            <el-button
              size="mini"
              type="text"
              v-if="changeReStatus(scope.row.statusText) === '申请退单'"
              @click="getRouterInfo(scope.row)"
            >退单查看</el-button>
            <el-button
              size="mini"
              type="text"
              class="text-warning"
              v-if="changeReStatus(scope.row.statusText) === '申请延期'"
              @click="getRouterInfo(scope.row)"
            >延期查看</el-button>
            <el-button
              size="mini"
              type="text"
              v-if="changeReStatus(scope.row.statusText) === '申请转单'"
              @click="getRouterInfo(scope.row)"
            >转单查看</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改巡检工单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body :close-on-click-modal="false" width="540px">
      <el-form ref="form" :model="form" :rules="rules" label-position="top" label-width="80px">
        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-form-item label="设备" prop="equipmentId">
              <el-select clearable v-model="form.equipmentId" placeholder="请选择设备" @change="selectChange">
                <el-option v-for="item in deviceList" :key="item.value" :value="item.value" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-form-item label="截止时间" prop="endDate">
              <el-date-picker clearable v-model="form.endDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择截止时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-form-item label="报修备注" prop="repairRemark">
              <el-input v-model="form.repairRemark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPatrol, getPatrol, delPatrol, addPatrol, updatePatrol } from "@/api/work/patrol";
import { listEquipment, getEquipment } from "@/api/resource/equipment";
import SearchFilter from '@/components/SearchFile'
import { changeReStatus } from '../../../utils/index'

export default {
  name: "Patrol",
  components: { SearchFilter },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡检工单信息表格数据
      patrolList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentId: null,
        projectId: null,
        areaId: null,
        position: null,
        endDate: null,
        owner: null,
        ownerUnit: null,
        unitArea: null,
        ownerPhone: null,
        contractDate: null,
        contractEndDate: null,
        projectManager: null,
        projectManagerPhone: null,
        repairPerson: null,
        repairPersonPhone: null,
        repairVideo: null,
        repairImg: null,
        repairPhone: null,
        repairRemark: null,
        status: null,
        reStatus: null,
        version: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      deviceList: [],
      deviceInfo: {}
    };
  },
  created() {
    this.getDeviceList()
    this.getList();
  },
  methods: {
    /** 设备列表 */
    async getDeviceList() {
      const res = await listEquipment({ deptId: this.$store.state.user.user.deptId, pageNum: -1 })
      if (res.rows.length) {
        this.deviceList = res.rows.map(item => {
          return {name: item.equipmentName, value: item.id}
        })
      } else {
        this.deviceList = []
      }
    },
    /** 查询设备详情 */
    async selectChange(val) {
      const res = await getEquipment(val)
      this.deviceInfo = res.data
    },
    /** 查询巡检工单信息列表 */
    getList() {
      this.loading = true;
      listPatrol({...this.queryParams, deptId: this.$store.state.user.user.deptId, orderByColumn: 'id', isAsc: 'desc' }).then(response => {
        this.patrolList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    changeStatus(val) {
      let str = ''
      switch(val) {
        case 0:
          str = '待处理'; break;
        case 1:
          str = '已处理'; break;
        default: break;
      }
      return str
    },
    changeReStatus(val) {
      if (!(val && val.length)) return
      let arr = JSON.parse(val)
      let obj = arr[arr.length - 1]
      if (obj.statusType === 2) {
        if (obj.status === 0) {
          return '申请退单'
        }
        if (obj.status === 1) {
          return '拒绝退单'
        }
        if (obj.status === 2) {
          return '同意退单'
        }
      }
      if (obj.statusType === 3) {
        if (obj.status === 0) {
          return '申请延期'
        }
        if (obj.status === 1) {
          return '拒绝延期'
        }
        if (obj.status === 2) {
          return '同意延期'
        }
      }
      if (obj.statusType === 4) {
        if (obj.status === 0) {
          return '申请转单'
        }
        if (obj.status === 1) {
          return '拒绝转单'
        }
        if (obj.status === 2) {
          return '同意转单'
        }
      }
    },
    /** 跳转详情 */
    getRouterInfo({ id }) {
      this.$router.push("/work/patrolQuery/" + id);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentId: null,
        projectId: null,
        areaId: null,
        position: null,
        endDate: null,
        owner: null,
        ownerUnit: null,
        unitArea: null,
        ownerPhone: null,
        contractDate: null,
        contractEndDate: null,
        projectManager: null,
        projectManagerPhone: null,
        repairPerson: null,
        repairPersonPhone: null,
        remark: null,
        repairVideo: null,
        repairImg: null,
        repairPhone: null,
        repairRemark: null,
        status: null,
        reStatus: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加巡检工单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPatrol(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改巡检工单信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePatrol({ ...this.form, deptId: this.$store.state.user.user.deptId }).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPatrol({...this.form, ...this.deviceInfo, deptId: this.$store.state.user.user.deptId }).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除巡检工单信息编号为"' + ids + '"的数据项？').then(function () {
        return delPatrol(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('work/patrol/export', {
        ...this.queryParams
      }, `patrol_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
